package ua.at.tsvetkov.samizdat.DB;

import ua.at.tsvetkov.samizdat.Const;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

	public static final String		DATABASE_NAME				= "samizdat.db";
	private static final int		DATABASE_VERSION			= 1;

	private static final String	BOOKS_DATABASE_CREATE	= "create table " + Const.BOOK + "(" + Const.ID + " integer primary key autoincrement, " + Const.OBJECT + " text not null, " + Const.AUTHOR
																					+ " text not null," + Const.TITLE + " text not null," + Const.TYPE + " text not null, " + Const.SIZE + " text not null," + Const.DATE
																					+ " integer not null," + Const.BALL + " real not null," + Const.COUNTER + " text not null, " + Const.ALL_JANR + " text not null,"
																					+ Const.ANNOT + " text not null," + Const.BOOK_GENRES + " text not null" + ");";

	private static final String	GENRES_DATABASE_CREATE	= "create table " + Const.GENRE + "(" + Const.ID + " integer primary key autoincrement, " + Const.NAME + " text not null, " + Const.CODE
																					+ " text not null, " + Const.AMOUNT + " text not null" + ");";

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// Method is called during creation of the database
	@Override
	public void onCreate(SQLiteDatabase database) {
		onGenresCreate(database);
		onBooksCreate(database);
	}

	// Method is called during an upgrade of the database,
	// e.g. if you increase the database version
	@Override
	public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
		onGenresUpgrade(database);
		onBooksUpgrade(database);
	}
	
	
	
	
	

	private void onBooksCreate(SQLiteDatabase database) {
		database.execSQL(BOOKS_DATABASE_CREATE);
	}

	private void onGenresCreate(SQLiteDatabase database) {
		database.execSQL(GENRES_DATABASE_CREATE);
	}

	private void onBooksUpgrade(SQLiteDatabase database) {
		database.execSQL("DROP TABLE IF EXISTS " + Const.BOOK);
		onCreate(database);
	}

	private void onGenresUpgrade(SQLiteDatabase database) {
		database.execSQL("DROP TABLE IF EXISTS " + Const.GENRE);
		onCreate(database);
	}

}
